package com.itheima.health.utils;


import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.Properties;

public class RedisUtil {
    private static JedisPool pool =null;
    static{
        //创建 连接池
        GenericObjectPoolConfig poolConfig=new GenericObjectPoolConfig();

        //读取配置文件
        Properties properties = new Properties();
        //加载数据
        try {
            properties.load(RedisUtil.class.getClassLoader().getResourceAsStream("redis.properties"));

            int minIdle =Integer.parseInt(properties.getProperty("redis.minIdle"));
            int maxTotal =Integer.parseInt(properties.getProperty("redis.maxTotal"));
            int maxIdle =Integer.parseInt(properties.getProperty("redis.maxIdle"));
            int maxWaitMillis =Integer.parseInt(properties.getProperty("redis.maxWaitMillis"));
            String host =properties.getProperty("redis.host");
            int port =Integer.parseInt(properties.getProperty("redis.port"));

            //初始化
            poolConfig.setMinIdle(minIdle);
            //最多
            poolConfig.setMaxTotal(maxTotal);
            //最大空闲
            poolConfig.setMaxIdle(maxIdle);

            //等待时间
            poolConfig.setMaxWaitMillis(maxWaitMillis);



            pool = new JedisPool(poolConfig,host,port);

        } catch (IOException e) {
            e.printStackTrace();
        }



    }

    public static Jedis getConnection(){



        //从池中借出来一个
        Jedis connection = pool.getResource();
        return connection;
    }

    public static void main(String[] args) {
        /*//创建 连接池
        GenericObjectPoolConfig poolConfig=new GenericObjectPoolConfig();
        //初始化
        poolConfig.setMinIdle(10);
        //最多
        poolConfig.setMaxTotal(100);
        //最大空闲
        poolConfig.setMaxIdle(50);

        //等待时间
        poolConfig.setMaxWaitMillis(2000);



        JedisPool pool = new JedisPool(poolConfig,"localhost",6379);


        //从池中借出来一个
        Jedis connection = pool.getResource();



        //进行业务操作
        Map<String, String> user1 = connection.hgetAll("user1");
        System.out.println(user1);

        //好借好还
        connection.close();*/


        Jedis connection = getConnection();

        String s = connection.get("test");
        System.out.println(s);
        connection.close();


    }
}
